package swd.work.trans_schema_form;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;
import com.pub.tools.PublicFunction;

public class Action {
    QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        String lstrUserNO = request.getSession().getAttribute("u_id").toString();
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        Business business = new Business(gqbnDAO,view.getDBAlias());

        String lstrResult = null;

        try {
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("getData")) {//透過ajax get data
                if(view.getDataKind().equals("tables")){
                    lstrResult=business.getTables(view.
                            getsys_no());
                }
                else if(view.getDataKind().equals("prgList")){
                    lstrResult=business.getPrgList(view.
                            getsys_no());
                }
                else if(view.getDataKind().equals("fields")){
                    lstrResult=business.getFields(view.
                            getdbf_no());
                }
                else if(view.getDataKind().equals("unusefrm")){
                    lstrResult=business.getUnUseFrmNo(view.getprg_no());
                }
                else if(view.getDataKind().equals("frm")){
                    lstrResult=business.checkFrmExists(view.getprg_no(),view.getfrm_no());
                    if(lstrResult.equals("0")){
                        lstrResult="";
                    }
                    else{
                        lstrResult=view.getMessage("frm_duplicate");
                    }
                }
                pageContext.getOut().clear();
                pageContext.getOut().write(lstrResult);
                pageContext.getOut().flush();
                return "login";
            }

            //user 未選取系統代號時，default 為環境參數中所設定的系統編號
            if (view.getsys_no() == null || view.getsys_no().trim().equals("")) {
                view.setsys_no(business.getDefaultSysNO(lstrUserNO));
            }

            if (view.getPrgAction() != null && view.getPrgAction().equals("transfer")) {
                lstrResult=business.transferSchema(view.getdbf_no(),view.gettrans_fields(),view.getprg_no(),view.getfrm_no());
                view.setOnloadScript("alert('共新增" + lstrResult + "個欄位成功');dbf_no_onchange(document.all.item('dbf_no'));" + view.getOnloadScript());
                view.setfrm_no(String.valueOf(PublicFunction.String_to_int(view.getfrm_no())+1));
            }

            return "";
        } catch (Exception e) {
            view.setOnloadScript("alert('Program error please contact IT.');");
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
